package cn.kgc.layui.servlet;

import cn.kgc.layui.dao.AuthorityDao;
import cn.kgc.layui.dao.RoleDao;
import cn.kgc.layui.dao.impl.AuthorityDaoImpl;
import cn.kgc.layui.dao.impl.RoleDaoImpl;
import cn.kgc.layui.entity.Authority;
import cn.kgc.layui.entity.Role;
import cn.kgc.layui.service.AuthorityService;
import cn.kgc.layui.service.RoleService;
import cn.kgc.layui.service.impl.AuthorityServiceImpl;
import cn.kgc.layui.service.impl.RoleServiceImpl;
import cn.kgc.layui.util.LayUiResult;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@WebServlet("/role/*")
public class RoleServlet extends BaseServlet<Role> {
    RoleService roleService=new RoleServiceImpl();
    private RoleDao roleDao = new RoleDaoImpl();

    /**
     * 查询 操作
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest req, HttpServletResponse resp,Role role) throws ServletException, IOException {

        int page = Integer.parseInt(req.getParameter("page"));
        int size = Integer.parseInt(req.getParameter("limit"));
        int start = (page-1)*size;
        LayUiResult<Role> result = roleService.findByPage(start, size,role);
        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(result);
        // 通过响应对象获取输出流
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(strResult);
    }

    /**
     * 新增操作
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void add(HttpServletRequest req, HttpServletResponse resp,Role role) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {


        int result=roleService.save(role);
        LayUiResult layUiResult;
        if(result>0){
            layUiResult=LayUiResult.builder().code(200).msg("保存成功").build();
        }else {
            layUiResult=LayUiResult.builder().code(500).msg("保存失败").build();
        }
        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(layUiResult);
        resp.getWriter().write(strResult);

    }

    /**
     * 修改
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void update(HttpServletRequest req, HttpServletResponse resp,Role role) throws ServletException, IOException{

        int result=roleService.update(role);
        LayUiResult layUiResult;
        if(result>0){
            layUiResult=LayUiResult.builder().code(200).msg("修改成功").build();
        }else {
            layUiResult=LayUiResult.builder().code(500).msg("修改失败").build();
        }

        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(layUiResult);
        // 通过响应对象获取输出流
        resp.getWriter().write(strResult);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp,Role role) throws ServletException, IOException{
        // 获取前端传递的参数
        String id= req.getParameter("id");
        String ids= req.getParameter("ids");
        int result=roleService.delete(id,ids);
        LayUiResult layUiResult;
        if(result>0){
            layUiResult=LayUiResult.builder().code(200).msg("删除成功").build();
        }else {
            layUiResult=LayUiResult.builder().code(500).msg("删除失败").build();
        }
        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(layUiResult);
        resp.getWriter().write(strResult);
    }

    public void setAuth(HttpServletRequest req, HttpServletResponse resp,Role role) throws ServletException, IOException{
       String roleId= req.getParameter("roleId");
       String authIds= req.getParameter("authIds");
       roleDao.saveRoleAuth(roleId,authIds);
        LayUiResult layUiResult=LayUiResult.builder().code(200).msg("保存成功").build();
        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(layUiResult);
        resp.getWriter().write(strResult);

    }
}
